<?php
namespace app\web\model;

/**
 * @package app\basic\model
 */
class Project extends Common 
{
    protected function setPositionNameAttr($v){
        return is_array($v) ? implode(',',$v) : $v;
    }
    protected function setImageAttr($v){
        return is_array($v) ? implode(',',$v) : $v;
    }
    protected function setProjectImagesAttr($v){
        return is_array($v) ? implode(',',$v) : $v;
    }
    protected function setServiceTimeAttr($v){
        $v = (int)$v;
        return 60*$v;
    }
    protected function getServiceTimeAttr($v){
        $v = (int)$v;
        return $v/60;
    }
    protected function getPositionNameAttr($v){
        return $v ? explode(',',$v) : [];
    }
    protected function getCreateTimeAttr($v){
        return date('Y-m-d',$v);
    }


    /**
     * 设置技师是否有可预约项目
     * [setIsProject description]
     * @param [type] $id [description]
     */
    public static function setIsProject($id)
    {
        $info = self::field('id,technician_id,shop_id,all_status')->get($id);
        if($info->technician_id > 0){  //个人技师项目
            $is_project = ProjectTechnician::alias('a')
                                            ->leftjoin('project b','b.id=a.project_id')
                                            ->where(['a.technician_id'=>$info->technician_id,'b.all_status'=>1,'b.status'=>1])
                                            ->count();

            $technician = Technician::get($info->technician_id);
            $technician->is_project = $is_project > 0 ? 1 : 0;
            $technician->save();
        }else{    //店铺项目
            $technician_ids = ProjectTechnician::where(['shop_id'=>$info->shop_id,'project_id'=>$id])->column('technician_id');
            foreach ($technician_ids as $key => $value) {
                $is_project = ProjectTechnician::alias('a')
                                            ->leftjoin('project b','b.id=a.project_id')
                                            ->where(['a.technician_id'=>$info->technician_id,'b.all_status'=>1,'b.status'=>1])
                                            ->count();
                $technician = Technician::get($info->technician_id);
                $technician->is_project = $is_project > 0 ? 1 : 0;
                $technician->save();
            }
        }
    }
}
